//
//  NSString+Extend.m
//  CoreCategory
//
//  Created by 成林 on 15/4/6.
//  Copyright (c) 2015年 沐汐. All rights reserved.
//

#import "NSString+Extend.h"
#import <CommonCrypto/CommonDigest.h>
static NSString *const tagStringOfr = @"!@#$%";
static NSString *const tagStringOfn = @"^&*()";
static NSString *const tagStringOftab = @"tab&^%$";
@implementation NSString (Extend)


/*
 *  时间戳对应的NSDate
 */
-(NSDate *)date{
    
    NSTimeInterval timeInterval=self.floatValue;
    
    return [NSDate dateWithTimeIntervalSince1970:timeInterval];
}

- (NSRange)allRange{
    return NSMakeRange(0, self.length);
}
- (NSRange)firstCharacterRange {
    return NSMakeRange(0, 1);
}
- (NSRange)lastCharacterRange {
    return NSMakeRange(self.length-1, 1);
}
- (NSString *)firstCharacter {
    if (self.length > 0) {
        return [self substringWithRange:[self firstCharacterRange]];
    }else {
        return @"";
    }
}
- (NSString *)lastCharacter {
    if (self.length > 0) {
        return [self substringWithRange:[self lastCharacterRange]];
    }else {
        return @"";
    }
}
- (NSString *)replacingOccurrencesOfrn{
    
    NSString *s1 = [self stringByReplacingOccurrencesOfString:@"\r" withString:tagStringOfr];
    NSString *s2 = [s1 stringByReplacingOccurrencesOfString:@"\n" withString:tagStringOfn];
    NSString *s3 = [s2 stringByReplacingOccurrencesOfString:@"\t" withString:tagStringOftab];
    return s3;
}
- (NSString *)escapedStringOfrn{
    NSString *s1 = [self stringByReplacingOccurrencesOfString:tagStringOfr withString:@"\r"];
    NSString *s2 = [s1 stringByReplacingOccurrencesOfString:tagStringOfn withString:@"\n"];
    NSString *s3 = [s2 stringByReplacingOccurrencesOfString:tagStringOftab withString:@"\t"];
    return s3;
}
- (NSString *)insertString:(NSString *)string inLocation:(NSUInteger)location{
    if (location < self.length) {
        return [NSString stringWithFormat:@"%@%@%@",[self substringToIndex:location],string,[self substringFromIndex:location]];
    }else{
        return self;
    }
}
+ (BOOL)isBlankString:(NSString *)str {
    if (str == nil || str == NULL || [str isKindOfClass:[NSNull class]] || [str length] == 0 || [str isEqualToString:@"(null)"]) {
        return YES;
    }
    return NO;
}


-(NSUInteger)textLength{
    
    NSUInteger asciiLength = 0;
    
    for (NSUInteger i = 0; i < self.length; i++) {
        
        
        unichar uc = [self characterAtIndex: i];
        
        asciiLength += isascii(uc) ? 1 : 2;
    }
    
    NSUInteger unicodeLength = asciiLength;
    
    return unicodeLength;
    
}
///查找计算指定字符串的个数
- (NSInteger)calculateNumSearchString:(NSString *)searchString option:(NSStringCompareOptions)options{
    if (self == nil)
        return 0;
    
    NSRange searchRange = NSMakeRange(0, self.length);
    NSRange range = NSMakeRange(0, 0);
    NSInteger num = 0;
    while(range.location != NSNotFound && searchRange.location < self.length)
    {
        range = [self rangeOfString:searchString options:options range:searchRange];
        //[attRange addRange:range]; //可将range加入数组并使用
        if (range.location != NSNotFound)
        {
            num++;
            searchRange.location = range.location + range.length;
            searchRange.length = self.length - searchRange.location;
        }
    }
    
    return num;
}

/* 返回结果即为包含每行文字的数组，行数即为count数
* 该方法主要是预先的计算出文本在UIlable等控件中的显示情况，从而进行一些其它的后续操作。
- (NSArray *)getLinesArrayOfStringInLabel:(NSString *)string font:(UIFont *)font andLableWidth:(CGFloat)lableWidth{
    
    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:string];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,lableWidth,100000));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    for (id line in lines) {
        CTLineRef lineRef = (__bridge  CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        NSString *lineString = [string substringWithRange:range];
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
        [linesArray addObject:lineString];
    }
    
    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);
    return (NSArray *)linesArray;
}
*/
@end
